package com.suredy.app.porject.ctrl.view;

import java.util.List;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.suredy.app.porject.constants.BidStatus;
import com.suredy.app.porject.constants.FlowStatus;
import com.suredy.app.porject.model.Bid;
import com.suredy.app.porject.model.FlowOfBid2User;
import com.suredy.security.model.User;

/**
 * 合同视图控制类
 * 
 * @author VIVID.G
 * @since 2015-12-25
 * @version v0.1
 */
public class ViewRoleCtrlForBid extends ViewRoleCtrl {

	private Bid bid;

	public ViewRoleCtrlForBid(Bid bid, User user) {
		super(user);
		this.bid = bid;
	}

	public boolean isCanSendToHetongZhuanze() {
		if (!this.isCanManageBid())
			return false;

		if (!this.isCreator() || !this.isBidGoing())
			return false;

		// 中标后可以发送给投标专责
		return this.isBidStatusIn(BidStatus.B0003_WIN);
	}

	public boolean isCanDoBidResult() {
		if (!this.isCanManageBid())
			return false;

		if (!this.isCreator() || !this.isBidGoing())
			return false;

		// 投标进行中可以进行投标结果的操作
		return !this.isBidHasResult();
	}

	public boolean isCanGetBackToubiaoBaozheng() {
		if (!this.isCanManageBid())
			return false;

		if (!this.isCreator())
			return false;

		// 没有投标结果之前，不允许回收
		if (!this.isBidHasResult())
			return false;

		// 投标保证金不可回收
		if (this.bid.getTbbzj() == null || this.bid.getTbbzj().getGaveBack() == null)
			return false;

		// 存在投标保证金，并且需要收回，且尚未收回
		return Boolean.FALSE.equals(this.bid.getTbbzj().getGaveBack());
	}

	public boolean isCanAddPayout() {
		if (!this.isCanManageBid())
			return false;

		if (!this.isCreator() || !this.isBidGoing())
			return false;

		// 流转前都可以添加
		return true;
	}

	public boolean isCanEditForm() {
		if (!this.isCanManageBid() || !this.isCanEditBid())
			return false;

		if (!this.isCreator() || !this.isBidGoing())
			return false;

		// 流转前都可以编辑
		return true;
	}

	public boolean isCanSignContract() {
		if (!this.isCanCreateMainContract())
			return false;

		// 流程不在“待签合同”环节
		if (!this.isUserActiveInFlow(FlowStatus.FS1001_BID_CONTRACT_CREATED))
			return false;

		return this.bid.getContract() == null;
	}

	public boolean isCanRemoveFile() {
		if (!this.isCanManageBid())
			return false;

		if (!this.isCreator() || !this.isBidGoing())
			return false;

		// 投标过程中
		return this.isUserActiveInFlow(FlowStatus.FS1000_BID_CREATED);
	}

	public boolean isCanUploadDoFile() {
		if (!this.isCanManageBid())
			return false;

		if (!this.isCreator() || !this.isBidGoing())
			return false;

		// 没有投标文件的时候可以上传投标文件
		return this.bid.getDoFile() == null;
	}

	public boolean isCanUploadServerFile() {
		if (!this.isCanManageBid())
			return false;

		if (!this.isCreator() || !this.isBidGoing())
			return false;

		// 没有投标文件的时候可以上传投标文件
		return this.bid.getServerFile() == null;
	}

	public boolean isCanUploadNoticeFile() {
		if (!this.isCanManageBid())
			return false;

		if (!this.isCreator() || !this.isBidGoing())
			return false;

		// 没有中标
		if (!this.isBidStatusIn(BidStatus.B0003_WIN))
			return false;

		// 没有投标文件的时候可以上传投标文件
		return this.bid.getNoticeFile() == null;
	}

	public boolean isCanUploadOtherFile() {
		if (!this.isCanManageBid())
			return false;

		if (!this.isCreator() || !this.isBidGoing())
			return false;

		// 流转中都可以添加附件
		return true;
	}

	public boolean isBidHasResult() {
		return this.isBidStatusIn(BidStatus.B0003_WIN, BidStatus.B0004_LOSE);
	}

	public boolean isCreator() {
		if (this.bid == null || this.bid.getFlowInfo() == null)
			return false;

		if (this.user == null || StringUtils.isBlank(this.user.getId()))
			return false;

		for (FlowOfBid2User flow : this.bid.getFlowInfo()) {
			if (flow == null || flow.getStatus() == null || flow.getUser() == null)
				continue;

			if (StringUtils.isBlank(flow.getUser().getId()))
				continue;

			// 必须处于新建环节
			if (!FlowStatus.FS1000_BID_CREATED.equals(flow.getStatus()))
				continue;

			if (flow.getUser().getId().equals(this.user.getId()))
				return true;
		}

		return false;
	}

	private boolean isBidGoing() {
		return this.isUserActiveInFlow(FlowStatus.FS1000_BID_CREATED);
	}

	private boolean isBidStatusIn(BidStatus... status) {
		if (this.bid == null || status == null || status.length <= 0)
			return false;

		return ArrayUtils.contains(status, this.bid.getStatus());
	}

	private boolean isUserActiveInFlow(FlowStatus... status) {
		if (this.bid == null || this.user == null || status == null || status.length <= 0)
			return false;

		if (StringUtils.isBlank(this.user.getId()))
			return false;

		List<FlowOfBid2User> fs = this.bid.getFlowInfo();

		if (fs == null || fs.isEmpty())
			return false;

		for (FlowOfBid2User f : fs) {
			// 非活动环节
			if (!Boolean.TRUE.equals(f.getActive()))
				continue;

			// 非指定环节中
			if (!ArrayUtils.contains(status, f.getStatus()))
				continue;

			// 为当前用户
			if (f.getUser() != null && this.user.getId().equals(f.getUser().getId()))
				return true;
		}

		return false;
	}

}
